package com.hnxxxy.controller;

import com.hnxxxy.pojo.Association;
import com.hnxxxy.pojo.User;
import com.hnxxxy.pojo.User_ass;
import com.hnxxxy.service.UserOpertionservice;
import com.hnxxxy.system.aop.IpUtil;
import com.hnxxxy.utils.Communal;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;

@Controller
@RequestMapping("/u")
public class UserOperationController {
    private static final Logger log= LogManager.getLogger(UserOperationController.class);

    @Autowired
    UserOpertionservice userOpertionservice;

    @Autowired
    Communal communal;

    /**
     * 前台ajax请求的处理
     * @param username 学号
     * @param password 密码
     * @return 带有登录成功或者失败信息的JSON
     */
    @RequestMapping(value="/ajaxLogin",method= RequestMethod.POST)
    @ResponseBody
    public String submitLogin(@RequestParam("username")String username, @RequestParam("password") String password,HttpServletRequest request) {
        log.debug("执行/ajaxLogin接口");
        communal.setIp(IpUtil.getIpAddr(request));
        return userOpertionservice.ajaxLogin(username,password);
    }

    /**
     * 查询用户信息
     * @return 用户信息的JSON
     */
    @RequestMapping(value="/s/userInfo",method= RequestMethod.POST)
    @ResponseBody
    public String queryUserInfo() {
        log.debug("执行/s/userInfo接口");
        return userOpertionservice.queryUserInfo();
    }

    /**
     * 修改或注册用户信息
     * @return 操作成功或失败的json信息
     */
    @RequestMapping(value="/ui/userInfo",method= RequestMethod.POST)
    @ResponseBody
    public String updateInsertUserInfo(@RequestBody Map<String, String> map) {
        log.debug("执行/ui/userInfo接口");
        String sjYz = communal.sjyz(map);
        if (sjYz != null) {
            return sjYz;
        }
        User user = new User();
        user.User(map.get("username"),map.get("sex"),map.get("clas"),map.get("admissiontime"),map.get("birthplace"),map.get("datebirth"),map.get("phone"),map.get("email"));
        return userOpertionservice.updateInsertUserInfo(user,map.get("password"),map.get("parameter"));
    }

    /**
     * 修改密码
     * @return 操作成功或失败的json信息
     */
    @RequestMapping(value="/u/userPassword",method= RequestMethod.POST)
    @ResponseBody
    public String updateUserPassword(@RequestParam("password") String password) {
        log.debug("执行/u/userPassword接口");
        if(password == null){return communal.resultMap(500,"请输入密码！");}
        return userOpertionservice.updateUserPassword(password);
    }

    /**
     * 查询用户申请社团记录信息
     * @return 用户申请社团记录信息
     */
    @RequestMapping(value="/s/userAssExa",method= RequestMethod.POST)
    @ResponseBody
    public String queryUserAssExa(int page,int limit) {
        log.debug("执行/s/userAssExa接口");
        return userOpertionservice.queryUserAssExa(page,limit);
    }

    /**
     * 放弃申请社团
     * @return 操作成功或失败的json信息
     */
    @RequestMapping(value="/d/userAssExa",method= RequestMethod.POST)
    @ResponseBody
    public String deleteUserAssExa(@RequestParam("id") int id) {
        log.debug("执行/d/userAssExa接口");
        if(id <= 0){return communal.resultMap(500,"请选择信息");}
        return userOpertionservice.deleteUserAssExa(id);
    }

    /**
     * 用户申请加入社团
     * @return 操作成功或失败的json信息
     */
    @RequestMapping(value="/i/userAssExa",method= RequestMethod.POST)
    @ResponseBody
    public String insertUserAssExa(@RequestParam("a_id") int a_id) {
        log.debug("执行/i/userAssExa接口");
        if(a_id <= 0){return communal.resultMap(500,"请选择信息");}
        return userOpertionservice.insertUserAssExa(a_id);
    }

    /**
     * 用户申请创建社团
     * @return 操作成功或失败的json信息
     */
    @RequestMapping(value="/i/Ass",method= RequestMethod.POST)
    @ResponseBody
    public String insertAss(@RequestBody Map<String, String> map, @RequestParam("img") MultipartFile img, HttpServletRequest request) throws IOException {
        log.debug("执行/i/Ass接口");
        String sjYz = communal.sjyz(map);
        if (sjYz != null) {
            return sjYz;
        }
        if(img == null){
            return communal.resultMap(500,"请选择图片");
        }
        String url = communal.path(img,request);
        String a_url = "";

        if(url != null){
            a_url = url;
        }else {
            return communal.resultMap(500,"上传图片失败");
        }
        Association association = new Association();
        association.Association3(map.get("a_name"),map.get("creationtime"),a_url);
        return userOpertionservice.insertAss(association);
    }

//    /**
//     * 注册
//     * @return 操作成功或失败的json信息
//     */
//    @RequestMapping(value="/i/userInfo",method= RequestMethod.POST)
//    @ResponseBody
//    public String insertUserInfo(@RequestBody Map<String, String> map) {
//        log.debug("执行/i/userAssExa接口");
//        String sjYz = communal.sjyz(map);
//        if (sjYz != null) {
//            return sjYz;
//        }
//        User user = new User();
//        user.User();
//        return userOpertionservice.insertUserAssExa(a_id);
//    }
}
